<template>
  <div class="card-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <span class="search-label">车牌号码：</span>
      <!-- 实现搜索功能，用v-model绑定到params.carNumber -->
      <!-- @clear="hSearch" 在点击由 clearable 属性生成的清空按钮时触发-->
      <el-input v-model="params.carNumber" clearable placeholder="请输入内容" class="search-main" @clear="hSearch" />
      <span class="search-label">车主姓名：</span>
      <!-- 实现搜索功能，用v-model绑定到params.personName -->
      <el-input v-model="params.personName" clearable placeholder="请输入内容" class="search-main" @clear="hSearch" />
      <span class="search-label">状态：</span>
      <el-select v-model="params.cardStatus" clearable @clear="hSearch">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
      <el-button type="primary" class="search-btn" @click="hSearch">查询</el-button>
    </div>
    <!-- 新增删除操作区域 -->
    <div class="create-container">
      <el-button type="primary" @click="$router.push('/addmonthcard')">添加月卡</el-button>
      <el-button @click="hdelBatch">批量删除</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table style="width: 100%" :data="list" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" />
        <el-table-column type="index" label="序号" :index="hIndex" />
        <el-table-column label="车主名称" prop="personName" />
        <el-table-column label="联系方式" prop="phoneNumber" />
        <el-table-column label="车牌号码" prop="carNumber" />
        <el-table-column label="车辆品牌" prop="carBrand" />
        <el-table-column label="剩余有效天数" prop="totalEffectiveDate" />
        <el-table-column label="状态" prop="cardStatus">
          <template #default="{row}">
            <el-tag v-if="row.cardStatus === 0" type="success">可用</el-tag>
            <el-tag v-if="row.cardStatus === 1" type="danger">已过期</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="180">
          <template #default="{row}">
            <el-button size="mini" type="text">续费</el-button>
            <el-button size="mini" type="text">查看</el-button>
            <el-button size="mini" type="text" @click="$router.push(`/addMonthCard?id=${row.id}`)">编辑</el-button>
            <el-button size="mini" type="text" @click="hdel(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page-container">
      <!-- current-page 页码 -->
      <!-- page-size 每一页显示多少条数据 -->
      <!-- current-change 页码切换时，该函数就会触发 -->
      <!-- total 月卡数据的总数 -->
      <el-pagination
        layout="total, prev, pager, next"
        :page-size="params.pageSize"
        :total="total"
        :current-page="params.page"
        background
        @current-change="handleCurrentChange"
      />
    </div>
    <!-- 添加楼宇 -->
    <el-dialog
      title="添加楼宇"
      width="580px"
    >
      <!-- 表单接口 -->
      <div class="form-container">
        <!-- <el-form ref="addForm" :model="addForm" :rules="addFormRules">
          <el-form-item label="楼宇名称" prop="name">
            <el-input v-model="addForm.name" />
          </el-form-item>
          <el-form-item label="楼宇层数" prop="floors">
            <el-input v-model="addForm.floors" />
          </el-form-item>
          <el-form-item label="在管面积" prop="area">
            <el-input v-model="addForm.area" />
          </el-form-item>
          <el-form-item label="物业费" prop="propertyFeePrice">
            <el-input v-model="addForm.propertyFeePrice" />
          </el-form-item>
        </el-form> -->
      </div>
      <template #footer>
        <el-button size="mini">取 消</el-button>
        <el-button size="mini" type="primary">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getCardListAPI, deleteCardAPI, deleteCardBatchAPI } from '@/api/card'
export default {
  name: 'CardPage',
  data() {
    return {
      params: {
        page: 1, // 页数
        pageSize: 2, // 每页显示条数
        carNumber: '', // 车牌号
        personName: '', // 车主姓名
        cardStatus: '' // 卡状态
      },
      total: 0, // 保存月卡数据总条数
      list: [], // 保存月卡列表数据
      selectList: [], // 选中的月卡列表
      options: [{
        value: 'null',
        label: '全部'
      }, {
        value: '0',
        label: '可用'
      }, {
        value: '1',
        label: '不可用'
      }]
    }
  },

  created() {
    this.getCardList()
  },
  methods: {
    // 获取月卡列表数据
    async getCardList() {
      try {
        const res = await getCardListAPI(this.params)
        // console.log(res.data.rows)
        this.list = res.data.rows
        // 将总条数赋值给total属性
        this.total = res.data.total
      } catch (error) {
        if (error.response) {
          this.$message.error(error.response.data.msg)
        }
      }
    },
    // 分页功能
    handleCurrentChange(val) {
      // 将val赋值给params.page
      this.params.page = val
      // 重新渲染
      this.getCardList()
    },
    // 渲染序号
    hIndex(index) {
      // console.log(index)
      // (当前页码 - 1) * 每页显示条数 + index + 1
      return (this.params.page - 1) * this.params.pageSize + index + 1
    },
    // 查询功能
    hSearch() {
      // 因为查询从第一页开始，所以把params.page重置为1
      this.params.page = 1
      // 调用获取月卡列表数据
      this.getCardList()
    },
    // 删除单个功能
    hdel(id) {
      // console.log(id)
      this.$confirm('确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        try {
          const res = await deleteCardAPI(id)
          // console.log(res)
          // 如果是演示数据不能删除(code=50000)
          if (res.code === 50000) {
            this.$message.error(res.msg)
          } else {
            // 删除成功后提示用户
            this.$message.success('删除成功')
            // 重新获取数据
            this.getCardList()
          }
        } catch (error) {
          if (error.response) {
            this.$message.error(error.response.data.msg)
          }
        }
      }).catch(() => {
        console.log('删除失败')
      })
    },
    handleSelectionChange(list) {
      console.log(list)
      this.selectList = list
    },
    // 批量删除功能
    hdelBatch() {
      // 判断是否选择月卡
      if (this.selectList.length === 0) {
        this.$message.warning('请选择要删除的月卡')
        // 没选择月卡就return
        return
      }
      this.$confirm('确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await deleteCardBatchAPI(this.selectList.map(item => item.id))
        // 重新渲染
        this.getCardList()
        // 删除成功提示用户
        this.$message.success('删除成功')
      }).catch(() => {
        this.$message.info('删除失败')
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.card-container {
  padding: 20px;
  background-color: #fff;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  padding-bottom: 20px;
  .search-main {
    width: 220px;
    margin-right: 10px;
  }
  .search-btn{
    margin-left:20px;
  }
}
.create-container{
  margin: 10px 0px;
}
.page-container{
  padding:4px 0px;
  text-align: right;
}
.form-container{
  padding:0px 80px;
}
</style>
